package drazing;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Controlador de entrada de datos
 *
 * Escucha las ordenes del usuario y se las pasa al motor de físicas
 * @author xcubo
 */

public class InputController implements KeyListener
{
    private boolean running;
    private boolean up, down, left, right;


    public InputController ()
    {
        running = true;
        up = false;
        down = false;
        left = false;
        right = false;
    }

    @Override
    public void keyTyped(KeyEvent ke)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public void keyPressed(KeyEvent ke)
    {
        lee_tecla(ke.getKeyCode(),true);
    }


    @Override
    public void keyReleased(KeyEvent ke) {
        lee_tecla(ke.getKeyCode(),false);
    }

    /**
     * Lee una tecla y registra un estado en el ControladorCarrera
     * @param keyCode Código de tecla pulsada
     * @param pressed Pulsada o liberada
     */
    private void lee_tecla(int keyCode, boolean pressed)
    {
        switch (keyCode)
        {
            case KeyEvent.VK_UP:
                        up = pressed;
                        break;

            case KeyEvent.VK_DOWN:
                        down = pressed;
                        break;

            case KeyEvent.VK_LEFT:
                        left = pressed;
                        break;

            case KeyEvent.VK_RIGHT:
                        right = pressed;
                        break;

            case KeyEvent.VK_ESCAPE:
                        running = false;
                        break;
        }
    }

    public boolean isRunning ()
    {
        return running;
    }
    public void exit ()
    {
        running = false;
    }

    public boolean[] getCommand()
    {
        boolean[] keys = new boolean[4];
        keys[0] = up;
        keys[1] = down;
        keys[2] = right;
        keys[3] = left;
        return keys;
    }
}
